package ch.netcetera.planningpoker.client.ui.widget.userlist;

import java.util.List;
import java.util.logging.Logger;

import ch.netcetera.planningpoker.client.model.ParticipantProxy;
import ch.netcetera.planningpoker.client.ui.events.UserListChangedEvent;
import ch.netcetera.planningpoker.client.ui.events.UserListChangedEventHandler;
import ch.netcetera.planningpoker.shared.GameRequestFactory;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Label;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.Proxy;

public class UserListPresenter extends
        Presenter<UserListPresenter.IView, UserListPresenter.IProxy> {

    private static final Logger logger = Logger.getLogger(UserListPresenter.class
            .getName());

    @ProxyStandard
    public interface IProxy extends Proxy<UserListPresenter> {

    }

    public interface IView extends View {
        
        HasWidgets getUserContainer();

    }

    private GameRequestFactory requestFactory;

    @Inject
    public UserListPresenter(EventBus eventBus, PlaceManager placeManager,
            IView view, IProxy proxy, GameRequestFactory requestFactory) {
        super(true, eventBus, view, proxy);
        this.requestFactory = requestFactory;
    }
    
    @Override
    protected void onBind() {
        getEventBus().addHandler(UserListChangedEvent.TYPE, new UserListChangedEventHandler() {
            
            @Override
            public void onUserListChanged(UserListChangedEvent event) {
                loadParticipants(event.getGameId());
            }
        });
    }
    
    private void loadParticipants(Long gameId) {
        requestFactory.getParticipantRequest().getParticipantsForGame(gameId).fire(new Receiver<List<ParticipantProxy>>() {

            @Override
            public void onSuccess(List<ParticipantProxy> participants) {
                displayParticipants(participants);
            }

        });
    }
    
    private void displayParticipants(List<ParticipantProxy> participants) {
        getView().getUserContainer().clear();
        if (participants.size() == 0) {
            getView().getUserContainer().add(new Label("So far no participants in this game.."));
        }
        for (final ParticipantProxy participant : participants) {
            UserView userView = new UserView();
            userView.setUserName(participant.getName());
            userView.getCloser().addClickHandler(new ClickHandler() {
                
                @Override
                public void onClick(ClickEvent event) {
                    removeParticipantFromGame(participant.getGameId(), participant.getId());
                }
            });
            getView().getUserContainer().add(userView);
        }
    }

    protected void removeParticipantFromGame(final Long gameId, Long participantId) {
        requestFactory.getParticipantRequest().removeParticipantFromGame(participantId).fire(new Receiver<Void>() {

            @Override
            public void onSuccess(Void response) {
                getEventBus().fireEventFromSource(new UserListChangedEvent(gameId), this);
            }
        });
    }

    @Override
    protected void revealInParent() {
        // Since this is not a place, we don't have to reveal
    }
}
